package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.AllProductCateRequest;
import com.cskaoyan.shopping.dto.AllProductCateResponse;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.NavListResponse;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 1.主页显示接口
 * 2.导航栏显示接口
 * 3.列表商品种类接口
 * @author CR
 * @since 2022/06/17 17:27
 */
@RestController
@RequestMapping("/shopping")
public class HomepageController {

    @Autowired
    IHomeService homeService;
    @Autowired
    IContentService contentService;
    @Autowired
    IProductCateService productCateService;

    @GetMapping("/homepage")
    public ResponseData getHomepage() {
        // 调用service处理请求
        HomePageResponse response = homeService.getHomepage();

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 调用失败
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("/navigation")
    public ResponseData getNavigation() {
        NavListResponse response = contentService.queryNavList();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("/categories")
    public ResponseData getCategories(String sort) {
        AllProductCateRequest cateRequest = new AllProductCateRequest();
        cateRequest.setSort(sort);
        AllProductCateResponse cateResponse = productCateService.getAllProductCate(cateRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(cateResponse.getCode())) {
            return new ResponseUtil().setData(cateResponse.getProductCateDtoList());
        }
        return  new ResponseUtil().setErrorMsg(cateResponse.getMsg());
    }


}
